#include "messager.h"
#include "ams.h"
#include "elog.h"
#include "lvgl/lvgl.h"
#include "rtc.h"

LV_IMG_DECLARE(icon_info);

LV_IMG_DECLARE(dial0_bg);
LV_IMG_DECLARE(dial0_black_bg);
LV_IMG_DECLARE(dial0_hours);
LV_IMG_DECLARE(dial0_hum_press_center);
LV_IMG_DECLARE(dial0_hum_press_needle);
LV_IMG_DECLARE(dial0_needle_center);
LV_IMG_DECLARE(dial0_seconds);
LV_IMG_DECLARE(dial0_minutes);

static lv_obj_t *img_dial_bg = NULL;
static lv_obj_t *img_dial_black_bg = NULL;
static lv_obj_t *img_dial_hours = NULL;
static lv_obj_t *img_dial_hum_center = NULL;
static lv_obj_t *img_dial_press_center = NULL;
static lv_obj_t *img_dial_hum_needle = NULL;
static lv_obj_t *img_dial_press_needle = NULL;
static lv_obj_t *img_dial_needle_center = NULL;
static lv_obj_t *img_dial_seconds = NULL;
static lv_obj_t *img_dial_minutes = NULL;

// ##
static void app_start(void *content)
{
}

// ##
static void app_stop(void *content)
{
}

// ##
static void app_prepare(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    AMS_UNUSE(activity);
}

// ##
static void *app_create(void *content)
{
    ams_activity_t *activity = (ams_activity_t *)content;
    activity->ui = lv_obj_create(NULL, NULL);
    img_dial_black_bg = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_black_bg, &dial0_black_bg);
    lv_obj_set_pos(img_dial_black_bg,
                    (LV_HOR_RES_MAX - lv_obj_get_width(img_dial_black_bg)) / 2,
                    (LV_VER_RES_MAX - lv_obj_get_height(img_dial_black_bg)) / 2);

    img_dial_bg = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_bg, &dial0_bg);
    lv_obj_set_pos(img_dial_bg,
                    (LV_HOR_RES_MAX - lv_obj_get_width(img_dial_bg)) / 2,
                    (LV_VER_RES_MAX - lv_obj_get_height(img_dial_bg)) / 2);

    img_dial_needle_center = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_needle_center, &dial0_needle_center);
    lv_obj_set_pos(img_dial_needle_center,
                    (LV_HOR_RES_MAX - lv_obj_get_width(img_dial_needle_center)) / 2,
                    (LV_VER_RES_MAX - lv_obj_get_height(img_dial_needle_center)) / 2);

    img_dial_hours = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_hours, &dial0_hours);
    lv_obj_set_pos(img_dial_hours, (LV_HOR_RES_MAX / 2) - 12, (LV_VER_RES_MAX / 2) - 106);
    lv_img_set_pivot(img_dial_hours, 12, 106);

    img_dial_minutes = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_minutes, &dial0_minutes);
    lv_obj_set_pos(img_dial_minutes, (LV_HOR_RES_MAX / 2) - 12, (LV_VER_RES_MAX / 2) - 134);
    lv_img_set_pivot(img_dial_minutes, 12, 134);

    img_dial_seconds = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_seconds, &dial0_seconds);
    lv_obj_set_pos(img_dial_seconds, (LV_HOR_RES_MAX / 2) - 2, (LV_VER_RES_MAX / 2) - 172);
    lv_img_set_pivot(img_dial_seconds, 2, 172);

    img_dial_hum_center = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_hum_center, &dial0_hum_press_center);
    lv_obj_set_pos(img_dial_hum_center, 92, 184);

    img_dial_press_center = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_press_center, &dial0_hum_press_center);
    lv_obj_set_pos(img_dial_press_center, 276, 184);

    img_dial_hum_needle = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_hum_needle, &dial0_hum_press_needle);
    lv_obj_set_pos(img_dial_hum_needle, (LV_HOR_RES_MAX *3 / 4) - 8, (LV_VER_RES_MAX / 2) - 38);
    lv_img_set_pivot(img_dial_hum_needle, 3, 38);

    img_dial_press_needle = lv_img_create(activity->ui, NULL);
    lv_img_set_src(img_dial_press_needle, &dial0_hum_press_needle);
    lv_obj_set_pos(img_dial_press_needle, (LV_HOR_RES_MAX / 4) + 3, (LV_VER_RES_MAX / 2) - 38);
    lv_img_set_pivot(img_dial_press_needle, 3, 38);

    return activity->ui;
}

// ##
static void *app_destroy(void *content)
{
    return NULL;
}

// ##
static void app_event_handle(void *event)
{
    ams_event_t *evt = event;
    switch (evt->msg_id)
    {
        case MSG_RTC_ALARM:
        {
            rtc_struct_t rtc_struct;
            rtc_time_get(&rtc_struct);
            lv_img_set_angle(img_dial_hours, (rtc_struct.hour % 12) *300 + rtc_struct.minutes *5);
            lv_img_set_angle(img_dial_minutes, rtc_struct.minutes *60);
            lv_img_set_angle(img_dial_seconds, rtc_struct.seconds *60);
            lv_img_set_angle(img_dial_hum_needle, rtc_struct.seconds *30 + 2700);
            lv_img_set_angle(img_dial_press_needle, rtc_struct.seconds *30 + 2700);
            break;
        }
        default:
        {
            log_i("unknown msg : %d", evt->msg_id);
            break;
        }
    }
}

// ##
static void app_gesture_handle(void *event)
{
    ams_gesture_t evt = *((ams_gesture_t *)event);
    switch(evt)
    {
        case GESTRUE_SLIDE_TO_TOP:
            break;
        case GESTRUE_SLIDE_TO_BUTTOM:
            break;
        case GESTRUE_SLIDE_TO_LEFT:
            ams_switch_by_name("app list", DIR_SLIDE_TO_LEFT);
            break;
        case GESTRUE_SLIDE_TO_RIGHT:
            break;
    }
}

// ##
static ams_ops_t ops =
{
    .prepare = app_prepare,
    .start = app_start,
    .stop = app_stop,
    .create = app_create,
    .destroy = app_destroy,
    .handler = app_event_handle,
    .gesture = app_gesture_handle,
};

// ##
const ams_activity_t ams_app_dial =
{
    .icon = &icon_info,
    .name = "dial",
    .ops = &ops,
};
